<template>
  <div 
    class="notification-item" 
    :class="{ 'is-unread': !notification.read }"
    @click="handleClick"
  >
    <!-- 左侧图标 -->
    <div class="item-icon" :style="{ color: typeColor }">
      <el-icon :size="24">
        <component :is="typeIcon" />
      </el-icon>
    </div>

    <!-- 中间内容 -->
    <div class="item-content">
      <div class="item-header">
        <span class="item-title">{{ notification.title }}</span>
        <el-badge v-if="!notification.read" is-dot class="unread-dot" />
      </div>
      <div class="item-text">{{ notification.content }}</div>
      <div class="item-time">{{ formattedTime }}</div>
    </div>

    <!-- 右侧操作按钮 -->
    <div class="item-actions">
      <el-tooltip content="标记已读" placement="top" v-if="!notification.read">
        <el-button 
          link 
          type="primary" 
          :icon="Check"
          @click.stop="handleMarkRead"
        />
      </el-tooltip>
      <el-tooltip content="删除" placement="top">
        <el-button 
          link 
          type="danger" 
          :icon="Delete"
          @click.stop="handleDelete"
        />
      </el-tooltip>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useNotificationStore } from '@/stores/notification'
import { Check, Delete, Bell, ChatDotRound, Warning } from '@element-plus/icons-vue'

// Props
const props = defineProps({
  notification: {
    type: Object,
    required: true
  }
})

// Emits
const emit = defineEmits(['read', 'delete'])

// Store
const notificationStore = useNotificationStore()

// 计算属性
const formattedTime = computed(() => {
  return notificationStore.formatTime(props.notification.time)
})

const typeIcon = computed(() => {
  const iconMap = {
    system: Bell,
    message: ChatDotRound,
    alert: Warning
  }
  return iconMap[props.notification.type] || Bell
})

const typeColor = computed(() => {
  return notificationStore.getTypeColor(props.notification.type)
})

// 方法
const handleClick = () => {
  if (!props.notification.read) {
    emit('read', props.notification.id)
  }
}

const handleMarkRead = () => {
  emit('read', props.notification.id)
}

const handleDelete = () => {
  emit('delete', props.notification.id)
}
</script>

<style scoped>
.notification-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
  background-color: #fff;
  margin-bottom: 8px;
  border: 1px solid #e4e7ed;
}

.notification-item:hover {
  background-color: #f5f7fa;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.notification-item.is-unread {
  background-color: #ecf5ff;
  border-color: #b3d8ff;
}

.notification-item.is-unread:hover {
  background-color: #d9ecff;
}

.item-icon {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background-color: rgba(64, 158, 255, 0.1);
}

.item-content {
  flex: 1;
  min-width: 0;
}

.item-header {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 4px;
}

.item-title {
  font-size: 14px;
  font-weight: 600;
  color: #303133;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.unread-dot {
  flex-shrink: 0;
}

.item-text {
  font-size: 13px;
  color: #606266;
  line-height: 1.5;
  margin-bottom: 6px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.item-time {
  font-size: 12px;
  color: #909399;
}

.item-actions {
  flex-shrink: 0;
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.3s;
}

.notification-item:hover .item-actions {
  opacity: 1;
}

/* 暗黑模式适配 */
html.dark .notification-item {
  background-color: #1e1e1e;
  border-color: #303030;
}

html.dark .notification-item:hover {
  background-color: #252525;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

html.dark .notification-item.is-unread {
  background-color: #1a2332;
  border-color: #2d5a8c;
}

html.dark .notification-item.is-unread:hover {
  background-color: #1f2a3a;
}

html.dark .item-title {
  color: #e5eaf3;
}

html.dark .item-text {
  color: #b3b3b3;
}

html.dark .item-time {
  color: #909399;
}

html.dark .item-icon {
  background-color: rgba(64, 158, 255, 0.2);
}
</style>

